﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Marmat.Forms;
using Marmat.Forms.Skin;
using Marmat.Foundation;

namespace Marmat.Template.WinForm
{
    /// <summary>
    /// 紫云鹿登录窗体
    /// </summary>
    public partial class DeerLoginForm : NoneBorderForm, ILoginForm
    {
        /// <summary>
        /// 构建紫云鹿登录窗体实例。
        /// </summary>
        public DeerLoginForm() : this(null)
        {
        }

        /// <summary>
        /// 构建紫云鹿登录窗体实例。
        /// </summary>
        /// <param name="login"></param>
        public DeerLoginForm(ILogin login)
        {
            InitializeComponent();
            this.Login = login;
            this.Icon = FileIconAPI.GetIcon(Application.ExecutablePath);
            this.DragParentForm();
        }

        /// <summary>
        /// 登录接口
        /// </summary>
        protected ILogin Login { get; }

        /// <summary>
        /// 获取返回的用户ID。
        /// </summary>
        protected string UserID { get; set; }
        /// <summary>
        /// 显示登录窗体。
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public bool ShowDialog(out string user_id)
        {
            user_id = default;
            var dialog = this.ShowDialog();
            bool res = dialog == DialogResult.OK;
            if (res)
                user_id = this.UserID;

            if (this.checkBox1.Checked && res)
            {
                Properties.Settings.Default.PassWord = this.PassWord.Value.Trim();
                Properties.Settings.Default.UserName = this.UserName.Value.Trim();
            }
            Properties.Settings.Default.RememberPassWord = this.checkBox1.Checked;
            Properties.Settings.Default.Save();
            return res;
        }

        #region 绘制代码
        private Color color = Color.FromArgb(113, 106, 186);

        /// <summary>
        /// 鼠标按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void loginBtn_MouseDown(object sender, MouseEventArgs e)
        {
            // this.loginBtn.Width = 252;
            //this.loginBtn.Height = 29;
            isDow = true;
            this.loginBtn.Invalidate();
        }

        /// <summary>
        /// 鼠标进入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void loginBtn_MouseEnter(object sender, EventArgs e)
        {
            this.color = Color.FromArgb(143, 136, 216);
            this.loginBtn.Invalidate();
        }
        private bool isDow = false;
        /// <summary>
        /// 鼠标抬起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void loginBtn_MouseUp(object sender, MouseEventArgs e)
        {
            isDow = false;
            this.loginBtn.Invalidate();
        }

        /// <summary>
        /// 鼠标离开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void loginBtn_MouseLeave(object sender, EventArgs e)
        {
            this.color = Color.FromArgb(113, 106, 186);
            this.loginBtn.Invalidate();
        }

        private void loginBtn_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            var rec = e.ClipRectangle;
            if (isDow)
                rec = new Rectangle(rec.X + 1, rec.Y + 1, rec.Width - 2, rec.Height - 2);
            var path = rec.GetRoundPath(5);
            using (var bush = new SolidBrush(this.color))
            {
                e.Graphics.FillPath(bush, path);
            }
            TextRenderer.DrawText(e.Graphics, "登录", this.loginBtn.Font, rec, Color.White, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
        }

        private void label2_Paint(object sender, PaintEventArgs e)
        {
            this.label2.Location = new Point((this.Width - this.label2.Width) / 2, this.label2.Location.Y);
        }

        private void label3_Paint(object sender, PaintEventArgs e)
        {
            this.label3.Location = new Point((this.Width - this.label3.Width) / 2, this.label3.Location.Y);
        }
        private void label4_Paint(object sender, PaintEventArgs e)
        {
            this.label4.Location = new Point((this.Width - this.label4.Width) / 2, this.label4.Location.Y);
        }
        #endregion

        /// <summary>
        /// 公司地址
        /// </summary>
        public string CompanyAddres
        {
            get => this._companyAddres;
            set
            {
                this._companyAddres = value;
                this.Invalidate();
            }
        }
        private string _companyAddres = "地址：XXX市XXX区XXX公司 邮编：000000";



        /// <summary>
        /// 登录系统
        /// </summary>
        private async void LoginSystem()
        {
            this.label4.Text = null;
            string username = this.UserName.Value.Trim();
            string password = this.PassWord.Value.Trim();
            if (string.IsNullOrEmpty(username))
            {
                this.label4.Text = "请输入用户名";
                this.UserName.Focus();
            }

            else if (string.IsNullOrEmpty(password))
            {
                this.label4.Text = "请输入密码";
                this.PassWord.Focus();
            }
            else
            {
                if (this.Login != null)
                {
                    try
                    {
                        this.loginBtn.Enabled = false;
                        this.UserID = await this.Login.Login(username, password);
                        this.DialogResult = DialogResult.OK;
                    }
                    catch (Exception ex)
                    {
                        this.label4.Text = ex.GetBaseException().Message;
                    }
                    finally
                    {
                        this.loginBtn.Enabled = true;
                    }
                }
            }
        }

        private void loginBtn_Click(object sender, EventArgs e)
        {
            this.LoginSystem();
        }

        private void DeerLoginForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) this.LoginSystem();
        }

        private void DeerLoginForm_Load(object sender, EventArgs e)
        {
            var result = Properties.Settings.Default.RememberPassWord;
            if (result)
            {
                this.UserName.Value = Properties.Settings.Default.UserName;
                this.PassWord.Value = Properties.Settings.Default.PassWord;
                this.checkBox1.Checked = result;
            }
        }

        private void DeerLoginForm_Paint(object sender, PaintEventArgs e)
        {
            //var addressRec = new Rectangle(29, 549, 761, 17);
            //var font = new Font("微软雅黑", 9F, FontStyle.Regular);
            //TextRenderer.DrawText(e.Graphics, this.CompanyAddres, font, addressRec, Color.Gray, TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter | TextFormatFlags.EndEllipsis);
            //var timeRec = new Rectangle(31, 565, 759, 17);
            //var assembly = (AssemblyCopyrightAttribute)AssemblyCopyrightAttribute.GetCustomAttribute(Assembly.GetEntryAssembly(), typeof(AssemblyCopyrightAttribute));
            //TextRenderer.DrawText(e.Graphics, assembly.Copyright, font, timeRec, Color.Gray, TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter | TextFormatFlags.EndEllipsis);
        }
    }
}
